package cn.edu.cqytxy.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * jdbc连接工具类
 *
 * @author yinjw
 */
public class JdbcUtils {

    /**
     * 数据库连接串
     */
    private static final String URL = "jdbc:mysql://localhost:3306/stu_data?allowMultiQueries=true";
    /**
     * 用户名
     */
    private static final String USERNAME = "root";

    /**
     * 密码
     */
    private static final String PASSWORD = "1234";

    /**
     * 提供一个Connection，用来每次都获取已经连接好的dbConnection
     */
    private static Connection connection;

    static {
        try {
            connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取已经连接好的DbConnection
     * @return  Connection
     */
    public static Connection getConnection() {
        return connection;
    }

    /**
     * 关闭连接
     */
    public static void close() {
        try {
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
